﻿/// <reference path="../Turbo/turbo_common.js" />

var _____a;



var $MarkupTemplate = TA.Turbo.Common.MarkupTemplate = function MarkupTemplate(name)
{
	this._name = name;
}
$MarkupTemplate.Inherit(BaseJsonDeserializable);

if (_____a)
{
	$MarkupTemplate.prototype = new BaseJsonDeserializable();
	$MarkupTemplate.prototype.BaseJsonDeserializable = new BaseJsonDeserializable();
}

TA.Turbo.Common.MarkupTemplate.rgxIsTableRow = new RegExp("^\\s*<tr", "i");

TA.Turbo.Common.MarkupTemplate.prototype._control = document.body;
TA.Turbo.Common.MarkupTemplate.prototype._name = "";
TA.Turbo.Common.MarkupTemplate.prototype._markup = "";
TA.Turbo.Common.MarkupTemplate.prototype._script = "";
//TA.Turbo.Common.MarkupTemplate.prototype._idPrefix = "";

// PUBLIC
TA.Turbo.Common.MarkupTemplate.prototype.get_Markup = function get_Markup() { return this._markup; }
TA.Turbo.Common.MarkupTemplate.prototype.get_Script = function get_Script() { return this._script; }
//TA.Turbo.Common.MarkupTemplate.prototype.get_IdPrefix = function get_IdPrefix() { return this._idPrefix; }

$MarkupTemplate.prototype.Deserialize = function Deserialize(json)
{
	this.BaseJsonDeserializable.Deserialize.call(this, json);

	this._ProcessMarkupTableRow();
}
$MarkupTemplate.prototype.DeserializeContent = function DeserializeContent(prShortName, value)
{
	switch (prShortName)
	{
		case "N": this._name = value; break;
		case "M": this._markup = value; break;
		case "S": this._script = value; break;
		//		case "I": this._idPrefix = value; break; 
	}
}
$MarkupTemplate.prototype._ProcessMarkupTableRow = function _ProcessMarkupTableRow()
{
	if ($MarkupTemplate.rgxIsTableRow.test(this._markup))
		this._markup = "<table><tbody>" + this._markup + "</tbody></table>";
}
$MarkupTemplate.prototype._ProcessMarkupAndScript = function ProcessMarkupAndScript(control)
{
	if (TA.Turbo.Common.GetControlServerId(control) != TA.Turbo.Common.GetControlClientId(control))
		this._markup = this._markup.Replace(TA.Turbo.Common.GetControlServerId(control), TA.Turbo.Common.GetControlClientId(control));
	if (TA.Turbo.Common.GetControlServerName(control) != TA.Turbo.Common.GetControlClientName(control))
		this._markup = this._markup.Replace(TA.Turbo.Common.GetControlServerName(control), TA.Turbo.Common.GetControlClientName(control));
	if (TA.Turbo.Common.GetControlServerId(control) != TA.Turbo.Common.GetControlClientId(control))
		this._script = this._script.Replace(TA.Turbo.Common.GetControlServerId(control), TA.Turbo.Common.GetControlClientId(control));
	if (TA.Turbo.Common.GetControlServerName(control) != TA.Turbo.Common.GetControlClientName(control))
		this._script = this._script.Replace(TA.Turbo.Common.GetControlServerName(control), TA.Turbo.Common.GetControlClientName(control));
}





var $MarkupTemplateCollection = TA.Turbo.Common.MarkupTemplateCollection = function MarkupTemplateCollection()
{
}
$MarkupTemplateCollection.Inherit(JsonCompatibleCollection);

if (_____a)
{
	$MarkupTemplateCollection.prototype = new JsonCompatibleCollection();
	$MarkupTemplateCollection.prototype.JsonCompatibleCollection = new JsonCompatibleCollection();
}
$MarkupTemplateCollection.prototype.CreateOneItem = function CreateOneItem(value)
{
	return new $MarkupTemplate();
}



TA.Turbo.Common.TemplateInstance = function TemplateInstance(control, markupTemplate, id, elements)
{
	if (Function.IsInherit(arguments)) return;

	if (_____a)
		markupTemplate = new MarkupTemplate();

	this._control = control;
	this._template = markupTemplate;
	this._id = id;
	this._validators = new Array();

	if (elements == null)
	{
		this._elements = new Array();
		this._Init_Template();
	}
	else
	{
		if (!(elements instanceof Array))
			elements = new Array(elements);

		this._elements = elements;
		this._Init_Elements();
	}
}

if (_____a)
{
	TA.Turbo.Common.TemplateInstance.prototype._control = document.body;
	TA.Turbo.Common.TemplateInstance.prototype._id = "";
	TA.Turbo.Common.TemplateInstance.prototype._template = new MarkupTemplate(document.getElementById("asd"), "a");
	TA.Turbo.Common.TemplateInstance.prototype._elements = new Array();
	TA.Turbo.Common.TemplateInstance.prototype._validators = new Array();
}
TA.Turbo.Common.TemplateInstance.prototype._visible = false;
TA.Turbo.Common.TemplateInstance.prototype._idPrefix = null;

TA.Turbo.Common.TemplateInstance.FindControl = function FindControl(find_id)
{
	return $(this._idPrefix + find_id);
};

TA.Turbo.Common.TemplateInstance.prototype.get_Elements = function get_Elements()
{
	return this._elements;
}
TA.Turbo.Common.TemplateInstance.prototype.get_Visible = function get_Visible()
{
	return this._visible;
}

TA.Turbo.Common.TemplateInstance.prototype._Init_Template = function _Init_Template()
{
	var newTemplate = document.createElement("div");
	newTemplate.style["display"] = "none";
	this._control.appendChild(newTemplate);
	newTemplate.innerHTML = this._template.get_Markup();


	var obj = this;
	var replaceId = this._control.id + TA.Turbo.Common.ClientIdSeparator + this._template._name + TA.Turbo.Common.ClientIdSeparator
		, replaceName = this._control.name + TA.Turbo.Common.UniqueIdSeparator + this._template._name + TA.Turbo.Common.UniqueIdSeparator
		, newId = this._control.id + TA.Turbo.Common.ClientIdSeparator + this._id + TA.Turbo.Common.ClientIdSeparator
		, newName = this._control.name + TA.Turbo.Common.UniqueIdSeparator + this._id + TA.Turbo.Common.UniqueIdSeparator;

	var getCont = function getCont()
	{
		return obj;
	};

	newTemplate.PerformRecursive(function ProcessElement(elem)
	{
		// if validator, add to list
		if (elem.getAttribute("ValidatedControlId") != null)
		{
			obj._validators.push(elem);
		}

		if (elem.id != null)
		{
			elem._serverId = elem.id;
			elem.id = elem.id.replace(replaceId, newId);
		}

		if (elem.name === undefined)
		{
			elem.name = elem.getAttribute("name"); // for non-form elements
		}
		if (elem.name != null)
		{
			elem._serverName = elem.name;
			elem.name = elem.name.replace(replaceName, newName);
			elem.setAttribute("name", elem.name); // for non-form elements
		}

		if (elem.value != null && typeof (elem.value) == "string") // take care of properties
		{
			elem.value = elem.value.Replace(replaceId, newId);
			elem.value = elem.value.Replace(replaceName, newName);
		}

		// ensure ID prefix used in FindControl
		if (obj._idPrefix == null)
		{
			var index = elem.id.indexOf(newId);
			if (index != -1)
				obj._idPrefix = elem.id.substr(0, index + newId.length);
		}
		elem._idPrefix = obj._idPrefix;

		// FindControl to find elements within the template
		elem.FindControl = TA.Turbo.Common.TemplateInstance.FindControl;
		elem.get_Container = getCont;
	});

	var elementsContainer = newTemplate;
	var isTable = false, tbody = newTemplate.FindFirst("TBODY");
	if (tbody != null)
	{
		if (tbody.parentNode.parentNode == newTemplate)
		{
			isTable = true;
			elementsContainer = tbody;
		}
	}

	for (var i = 0; i < elementsContainer.get_ChildNodes().length; ++i)
	{
		var childNode = elementsContainer.get_ChildNodes()[i];
		if (childNode.nodeName == "#text" && isTable) continue;
		this._elements.push(childNode);
	}

	var script = this._template._script.Replace(replaceId, newId).Replace(replaceName, newName);
	eval(script);
}
TA.Turbo.Common.TemplateInstance.prototype._Init_Elements = function _Init_Elements()
{
	var replaceId = this._control.id + TA.Turbo.Common.ClientIdSeparator + this._template._name + TA.Turbo.Common.ClientIdSeparator
		, replaceName = this._control.name + TA.Turbo.Common.UniqueIdSeparator + this._template._name + TA.Turbo.Common.UniqueIdSeparator
		, newId = this._control.id + TA.Turbo.Common.ClientIdSeparator + this._id + TA.Turbo.Common.ClientIdSeparator
		, newName = this._control.name + TA.Turbo.Common.UniqueIdSeparator + this._id + TA.Turbo.Common.UniqueIdSeparator;

	var obj = this;
	var getCont = function getCont()
	{
		return obj;
	};

	for (var iElem = 0; iElem < this._elements.length; ++iElem)
	{
		var topLevelElem = this._elements[iElem];

		topLevelElem.PerformRecursive(function ProcessElement(elem)
		{
			if (elem.get_Container == null)
			{
				// if validator, add to list
				if (elem.getAttribute("ValidatedControlId") != null)
				{
					obj._validators.push(elem);
				}

				if (elem.id != null)
				{
					elem._serverId = elem.id.replace(newId, replaceId);
				}

				if (elem.name != null)
				{
					elem._serverName = elem.name.replace(newName, replaceName);
				}

				if (elem.value != null && typeof (elem.value) == "string") // take care of properties
				{
					elem.value = elem.value.Replace(replaceId, newId);
					elem.value = elem.value.Replace(replaceName, newName);
				}

				// ensure ID prefix used in FindControl
				if (obj._idPrefix == null)
				{
					var index = elem.id.indexOf(newId);
					if (index != -1)
						obj._idPrefix = elem.id.substr(0, index + newId.length);
				}
				elem._idPrefix = obj._idPrefix;

				// FindControl to find elements within the template
				elem.FindControl = TA.Turbo.Common.TemplateInstance.FindControl;
				elem.get_Container = getCont;
			}
		}, true);

	}
}
TA.Turbo.Common.TemplateInstance.prototype.Show = function Show(container, refElem)
{
	if (refElem != null)
	{
		for (var i = 0; i < this._elements.length; ++i)
		{
			container.insertBefore(this._elements[i], refElem);
		}
	}
	else
	{
		for (var i = 0; i < this._elements.length; ++i)
		{
			container.appendChild(this._elements[i]);
		}
	}

	this._visible = true;

}
TA.Turbo.Common.TemplateInstance.prototype.Hide = function Hide()
{
	if (this._visible)
	{
		for (var i = 0; i < this._elements.length; ++i)
			this._elements[i].parentNode.removeChild(this._elements[i]);

		this._visible = false;
	}
}
TA.Turbo.Common.TemplateInstance.prototype.FocusFirst = function FocusFirst()
{
	for (var i = 0; i < this.get_Elements().length; ++i)
	{
		var elem = this.get_Elements()[i];
		if (elem.tagName == null) continue;

		var inputs = elem.Find("INPUT|SELECT|TEXTAREA");
		if (inputs.length > 0)
		{
			try
			{
				inputs[0].focus();
				break;
			}
			catch (e) { }
		}
	}
}
TA.Turbo.Common.TemplateInstance.prototype.get_IsValid = function get_IsValid()
{
	var isValid = true;
	for (var iValidator = 0; iValidator < this._validators.length; ++iValidator)
		isValid = isValid && this._validators[iValidator].get_Object().get_IsValid();
	return isValid;
}
TA.Turbo.Common.TemplateInstance.prototype.FindControl = function FindControl(id)
{
	return document.getElementById(this._idPrefix + id);
}